package com.ujs.edu.demo.controller;

import com.ujs.edu.demo.dao.BcAdminMapper;
import com.ujs.edu.demo.dataobject.BcAdmin;
import com.ujs.edu.demo.util.MSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private BcAdminMapper bcAdminMapper;

    //进入登录页面
    @RequestMapping(path = "/tologin")
    public ModelAndView login(){
        ModelAndView view=new ModelAndView();
        view.setViewName("login");
        return view;
    }
    //登录验证
    @RequestMapping(path = "login")
    public RedirectView login(@RequestParam("roleId") Integer roleId,
                              @RequestParam("input")String input,
                              @RequestParam("password") String password,
                              HttpSession session) throws Exception {

        RedirectView view=new RedirectView();
        if (roleId==null){
            view.setUrl("/tologin");
            return view;
        }else if(roleId==3){
            String page="/bookfirst";
         BcAdmin bcAdmin= checklogin(input,password,page,roleId);
            if (bcAdmin!=null){
                view.setUrl(page);
                session.setAttribute("bcAdmin",bcAdmin);
            }else {
                view.setUrl("/tologin");
            }
        }else {
            String page="/main";
           BcAdmin bcAdmin= checklogin(input,password,page,roleId);
           if(bcAdmin!=null){
               view.setUrl(page);
               session.setAttribute("bcAdmin",bcAdmin);
           }else {
               view.setUrl("/tologin");
           }
        }
        return view;
    }

    public BcAdmin checklogin(String input,String password,String page,Integer roleId) throws Exception {

        String email = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        String telephone = "^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";
        BcAdmin bcAdmin=bcAdminMapper.selectByUsername(input,roleId);

        if (bcAdmin!=null){
            if (MSUtil.verify(password,"xhj",bcAdmin.getPassword())){
                return bcAdmin;
            }else {
                return null;
            }
        }
        else if(input.matches(telephone)){
            BcAdmin bcAdmin1=bcAdminMapper.selectByMobile(input);
            if(bcAdmin1!=null&&MSUtil.verify(password,"xhj",bcAdmin1.getPassword())){
                return bcAdmin1;
            }else {
                return null;
            }
        }
        else if(input.matches(email)){
            BcAdmin bcAdmin1=bcAdminMapper.selectByEmail(input,roleId);
            if (bcAdmin1!=null&&MSUtil.verify(password,"xhj",bcAdmin1.getPassword())){
                return bcAdmin1;
            }else {
                return null;
            }
        }
        else {
            return null;
        }
    }

    @RequestMapping(path = "/loginout")
    public RedirectView loginOut(HttpSession session) {
        session.removeAttribute("bcAdmin");
        RedirectView view=new RedirectView();
        view.setUrl("/index");
        return view;
    }

    @RequestMapping(path = "/getSession")
    @ResponseBody
    public String getSession(@SessionAttribute("bcAdmin")int adminId){
        return "bcAdmin"+adminId;
    }
}
